package cn.peach.hrm.service.impl;

import cn.peach.hrm.constants.BaseConstants;
import cn.peach.hrm.constants.ErrorCode;
import cn.peach.hrm.domain.Employee;
import cn.peach.hrm.domain.Login;
import cn.peach.hrm.domain.Tenant;
import cn.peach.hrm.dto.LoginMealDto;
import cn.peach.hrm.dto.TenantRegisterDto;
import cn.peach.hrm.exception.GlobalException;
import cn.peach.hrm.feign.client.LoginFeignClient;
import cn.peach.hrm.mapper.TenantMapper;
import cn.peach.hrm.service.IEmployeeService;
import cn.peach.hrm.service.ITenantService;
import cn.peach.hrm.util.AjaxResult;
import cn.peach.hrm.util.AssertUtil;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;

import javax.validation.Valid;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author peach.ja
 * @since 2021-11-22
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private LoginFeignClient loginFeignClient;

    @Autowired
    private IEmployeeService employeeService;

    public static void main(String[] args) {
        Assert.hasLength("000","错误啦" );
    }

    @Transactional(rollbackFor = {Exception.class})
    @Override
    public void entering(@Valid TenantRegisterDto dto) {
        Login login = dto.getLogin();
        Long mealId = dto.getMealId();
        Tenant tenant = dto.getTenant();
        Employee employee = dto.getEmployee();

        AssertUtil.isNotEmpty(login.getUsername(),ErrorCode.LOGIN_USERNAME_IS_NULL.getFullMessage());
        AssertUtil.isNotEmpty(login.getPassword(),ErrorCode.LOGIN_PASSWORD_IS_NULL.getFullMessage());
        AssertUtil.isNotEmpty(tenant.getCompanyName(),ErrorCode.TENANT_NAME_IS_NULL.getFullMessage());

        // 远程保存Login表 保存机构和套餐关系表 密码加密
        LoginMealDto loginMealDto = new LoginMealDto();
        login.setType(BaseConstants.Login.TYPE_SYSTEM);
        loginMealDto.setLogin(login);
        loginMealDto.setMealId(mealId);
        AjaxResult ajaxResult = loginFeignClient.saveLoginAndMeal(loginMealDto);
        if (ajaxResult.isSuccess() || ajaxResult.getResultObj() == null) {
            throw new RuntimeException("登录用户保存失败");
        }
        Long loginId = Long.valueOf(ajaxResult.getResultObj().toString());

        Employee employee1 = new Employee();
        employee1.setEmail(employee.getEmail());
        employee1.setTel(employee.getTel());
        employee1.setLoginId(employee.getLoginId());
        Date date = new Date();
        employee1.setInputTime(new Date());
        employee1.setType(BaseConstants.Employee.TYPE_PLATFORM_ADMIN);
        employeeService.insert(employee1);

        // 保存机构
        tenant.setRegisterTime(date);
        tenant.setState(BaseConstants.Tenant.STATE_WAIT_AUDIT);
        tenant.setAdminId(employee.getId());

        insert(tenant);
    }
}
